package com.book.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.business.entity.BookSell;
import com.book.business.mapper.BookSellMapper;
import com.book.business.service.BookSellService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 盛佳帅
 * @Description:  20173953
 */

@Service
public class BookSellServiceImpl extends ServiceImpl<BookSellMapper, BookSell> implements BookSellService {

    @Autowired
    BookSellMapper bookSellMapper;

    @Override
    public List<BookSell> findBookSell(BookSell bookSell) {
        QueryWrapper<BookSell> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookSell.getPubid())) {
            queryWrapper.like("pubid", bookSell.getPubid());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookSell> pageBookSell(QueryRequest request, BookSell bookSell) {
        QueryWrapper<BookSell> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookSell.getBookname())) {
            queryWrapper.like("bookname", bookSell.getBookname());
        }
        if (StringUtils.isNotEmpty(bookSell.getType())) {
            queryWrapper.eq("type", bookSell.getType());
        }
        Page<BookSell> bookSellIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookSellIPage, queryWrapper);
    }
    @Override
    public IPage<BookSell> pageBookSell(QueryRequest request, BookSell bookSell,String status) {
        QueryWrapper<BookSell> queryWrapper = new QueryWrapper<>();
        if ("1".equals(status)) {
            queryWrapper.inSql("id", "SELECT bookid FROM shelves_apply WHERE status = 1");
        }
        if (StringUtils.isNotEmpty(bookSell.getBookname())) {
            queryWrapper.like("bookname", bookSell.getBookname());
        }
        if (StringUtils.isNotEmpty(bookSell.getType())) {
            queryWrapper.eq("type", bookSell.getType());
        }
        Page<BookSell> bookSellIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookSellIPage, queryWrapper);
    }

    @Override
    public void addBookSell(BookSell bookSell) {
        this.save(bookSell);
    }

    @Override
    public void updateBookSell(BookSell bookSell) {
        this.saveOrUpdate(bookSell);
    }

    @Override
    public void deleteBookSell(String[] bookSell) {
        List<String> list = Arrays.asList(bookSell);
        this.removeByIds(list);
    }
}